package com.springark.ms.sampleorders.service;

import com.springark.ms.commons.MsApiResponse;
import com.springark.ms.sampleorders.model.OrdersResult;
import feign.FeignException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hankchan
 * @since 2019/5/10
 */
@Service
public class OrdersService {

    @Resource
    private GoodsFeignService goodsService;

    public List<OrdersResult> get(String requestId) {
        List<OrdersResult> results = new ArrayList<>();
        List<String> uniques = null;
        try {
            MsApiResponse<List<String>> response = goodsService.get(requestId);
            uniques = response.getData();
        } catch (FeignException e) {
            e.printStackTrace();
            // 微服务调用异常
            throw new RuntimeException("微服务调用异常");
        }
        if(null == uniques || uniques.isEmpty()) {
            return results;
        }
        for (int i = 0; i < uniques.size(); i++) {
            OrdersResult result = new OrdersResult();
            result.setOrderId(uniques.get(i));
            result.setGoodsId((long) i);
            results.add(result);
        }
        return results;
    }
}
